package com.config;

import com.alibaba.fastjson.JSONObject;
import com.bean.ResponseBean;
import com.pojo.Admin;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Admin admin = (Admin)request.getSession().getAttribute("admin");
        if (admin!=null)
        {
            return true;
        }
        response.setContentType("text/html;charset=utf-8");
        ResponseBean responseBean = new ResponseBean(403,"没有权限!",null);
        String json = JSONObject.toJSONString(responseBean);
        response.getWriter().println(json);
        return false;
    }
}
